package com.lbs.demo.controllers;

import com.lbs.demo.dao.UserDao;
import com.lbs.demo.entities.UsersEntity;
import com.sun.deploy.net.HttpResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/")
public class UserController {

    @Resource
    UserDao userDao = new UserDao();

    @RequestMapping(value = "login", method = RequestMethod.GET)
    public String userLogin(ModelMap map) {
        return "login";
    }

    @RequestMapping(value = "register", method = RequestMethod.GET)
    public String userRegister(ModelMap map){
        return "register";
    }

    @RequestMapping(value = "loginSubmit", method = RequestMethod.POST)
    public String loginSubmit(ModelMap map, @RequestParam String username, @RequestParam String password, HttpSession session) {

        if (username.equals("") || password.equals("")) {
            map.addAttribute("error", "用户名或密码不能为空");
            return "login";
        }

        UsersEntity user = userDao.findUser(username, password);
        if (user == null) {
            map.addAttribute("error", "错误的用户名或者密码");
            return "login";
        } else {
            session.setAttribute("user", user);
            return "index";
        }
    }

    @RequestMapping(value = "registerSubmit", method = RequestMethod.POST)
    public String registerSubmit(ModelMap map, @RequestParam String username, @RequestParam String email, @RequestParam String password1, @RequestParam String password2) {
        if (username.equals("") || password1.equals("") || email.equals("")) {
            map.addAttribute("error", "用户名，邮箱或密码不能为空");
            return "register";
        }

        if (!password1.equals(password2)) {
            map.addAttribute("error", "两次输入的密码不一致");
            return "register";
        }

        UsersEntity user = new UsersEntity();
        user.setName(username);
        user.setPassword(password1);
        user.setEmail(email);

        try{
            userDao.addUser(user);
            return "index";
        }catch (Exception e){
            //e.printStackTrace();
            map.addAttribute("error", "重复的用户，注册失败");
            return "register";
        }
    }

    @RequestMapping(value = "logOut", method = RequestMethod.GET)
    public String logOut(ModelMap map, HttpSession session){
        session.removeAttribute("user");
        return "login";
    }


    @RequestMapping(value = "users", method = RequestMethod.GET)
    public String listUsers(ModelMap map) {
        map.addAttribute("users", userDao.getAllUsers());
        return "users";
    }


    @RequestMapping(value = "adduser", method = RequestMethod.GET)
    public String addUser(ModelMap map) {
        UsersEntity user = new UsersEntity();
        user.setName("toyking");
        user.setAge((byte) 20);
        map.addAttribute("user", user);
        return "adduser";
    }


    @RequestMapping(value = "result", method = RequestMethod.POST)
    public String getResult(ModelMap map, @RequestParam String name, @RequestParam int age) {
        map.addAttribute("name", name);
        map.addAttribute("age", age);

        UsersEntity user = new UsersEntity();
        user.setName(name);
        user.setEmail("mail_added");
        user.setPassword("test");
        user.setAge((byte) age);
        userDao.addUser(user);

        return "name";
    }
}
